/**
 * Created with IntelliJ IDEA.
 * Description:岛屿数量
 * User: wangxin
 * Date: 2024-12-24
 * Time: 22:14
 */
public class Test2 {
    public void dfs(char[][] grid,int i,int j){
        int n = grid.length;
        int m = grid[0].length;
        grid[i][j] = '0';
        if(i - 1>=0&&grid[i-1][j]=='1'){
            dfs(grid,i-1,j);
        }
        if(i + 1 < n&&grid[i+1][j]=='1'){
            dfs(grid,i+1,j);
        }
        if(j - 1>=0&&grid[i][j-1]=='1'){
            dfs(grid,i,j-1);
        }
        if(j+1<m&&grid[i][j+1]=='1'){
            dfs(grid,i,j+1);
        }


    }
    public int solve (char[][] grid) {
        // write code here
        int n = grid.length;
        int m = grid[0].length;
        int count = 0;
        for(int i = 0;i < n;i++){
            for(int j = 0;j < m;j++){
                if(grid[i][j]=='1'){
                    count++;
                    dfs(grid,i,j);
                }
            }
        }
        return count;
    }
}
